package de.uni.kassel.campusappver3.view;

import android.app.Activity;
import android.os.Bundle;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.widget.ImageView;
import android.widget.TextView;
import de.uni.kassel.campusappver3.R;
import de.uni.kassel.campusappver3.model.Branch;
/**
 * branches == Fachgebiet
 * @author Tohme
 *
 */
public class BranchesActivity extends Activity {

	public String key; 
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_branches);
		
		//get the branch key from the bundle to
		//show the right information
		String branchKey;
		if (savedInstanceState == null) {
		    Bundle extras = getIntent().getExtras();
		    if(extras == null) {
		    	branchKey = null;
		    } else {
		    	branchKey = extras.getString("Fachgebiet");
		    	savedInstanceState = extras;
		    }
		} else {
			branchKey = (String) savedInstanceState.getSerializable("Fachgebiet");
		}
	
		fillContent(branchKey);	
		key = branchKey;
	}
	
	/**
	 * save the bundle (e.g. on orientation changed)
	 */
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		outState.putString("Fachgebiet", key);
		super.onSaveInstanceState(outState);
	}
	
	
	//setting the right information in Views
	private void fillContent(String branchKey){
		Branch branch;
		de.uni.kassel.campusappver3.model.Resources resSingelton = 
				de.uni.kassel.campusappver3.model.Resources.getInstance();
		
		if(resSingelton.hasBranch(branchKey)){
			branch = resSingelton.getBranch(branchKey);
			
			//set name
			TextView branchName = (TextView) findViewById(R.id.branchName);
			branchName.setText(branch.getName());
			
			// set email
			TextView email = (TextView) findViewById(R.id.email);
			email.setText(branch.getEmail());
			
			// set url
			TextView url = (TextView) findViewById(R.id.url);
			url.setText(Html.fromHtml(
					"<a href=\""+branch.getUrl()+"\""+">"+branch.getName()+"</a> "));
			url.setMovementMethod(LinkMovementMethod.getInstance());
			// set profname
			TextView pn = (TextView) findViewById(R.id.profName);
			pn.setText(branch.getProfName());
			
			// set tel
			TextView tel = (TextView) findViewById(R.id.tel);
			tel.setText(branch.getTel());
			
			// set contact info
			TextView contactInfo = (TextView) findViewById(R.id.contactInfo);
			contactInfo.setText(branch.getContact());
			
			// set contact info
			ImageView image = (ImageView) findViewById(R.id.branchLogo);
			image.setImageResource(branch.getLogo());
			
			
		}
		else
			this.finish();
	}

}